var util = require('util');

/**
 * Domain class. Contains all the necessary information to create/update/delete/export a contact.
 *
 * @constructor
 */
function CWContact () {
    var defaultValues;

    defaultValues = {
        name:         '',
        firstname:     '',
        address:      '',
        organisation: [],
        title:        [],
        mail:         '',
        tel:          ''
    };

    this._name         = defaultValues.name;
    this._firstname     = defaultValues.firstname;
    this._address      = defaultValues.address;
    this._organisation = defaultValues.organisation;
    this._title        = defaultValues.title;
    this._mail         = defaultValues.mail;
    this._tel          = defaultValues.tel;
}

/**
 * Getter/Setter for a CWContact name.
 *
 * @param name When provided, this method sets name as the new CWContact instance name.
 * @returns {*} Returns the CWContact instance the _name attribute refers to when a name is provided.
 * When it is not, it directly returns the instance name value.
 */
CWContact.prototype.name = function (name) {
    if (name === undefined) {
        return this._name;
    }

    if (name && typeof name !== 'string') {
        throw new TypeError('Name must be a string.');
    }

    this._name = name;

    return this;
};

/**
 * Getter/Setter for a CWContact firstname
 * @param firstname When provided, this method sets firstname as the new CWContact instance firstname.
 * @returns {*} Returns the CWContact instance the _fullname attribute refers to when a firstname is provided.
 * When it is not, it directly returns the instance firstname value.
 */
CWContact.prototype.firstname = function (firstname) {
    if (firstname === undefined) {
        return this._firstname;
    }

    if (firstname && typeof firstname !== 'string') {
        throw new TypeError('firstname must be a string.');
    }

    this._firstname = firstname;

    return this;
};

/**
 * Getter/Setter for a CWContact address.
 *
 * @param address When provided, this method sets address as the new CWContact instance address.
 * @returns {*} Returns the CWContact instance the _address attribute refers to when an address is provided.
 * When it is not, it directly returns the instance address value.
 */
CWContact.prototype.address = function (address) {
    if (address === undefined) {
        return this._address;
    }

    if (address && typeof address !== 'string') {
        throw new TypeError('Address must be a string.');
    }

    this._address = address;

    return this;
};

/**
 * Getter/Setter for a CWContact organisation.
 *
 * @param organisation When provided, this method sets organisation as the new CWContact instance organisation.
 * @returns {*} Returns the CWContact instance the _organisation attribute refers to when an organisation is provided.
 * When it is not, it directly returns the instance organisation value.
 */
CWContact.prototype.organisation = function (organisation) {
    if (organisation === undefined) {
        return this._organisation;
    }

    if (!util.isArray(organisation)) {
        throw new TypeError('Organisation must be an array.');
    }

    this._organisation = organisation;

    return this;
};

/**
 * Getter/Setter for a CWContact title.
 *
 * @param title When provided, this method sets title as the new CWContact instance title .
 * @returns {*} Returns the CWContact instance the _title attribute refers to when a title is provided.
 * When it is not, it directly returns the instance title value.
 */
CWContact.prototype.title = function (title) {
    if (title=== undefined) {
        return this._title;
    }

    if (!util.isArray(title)) {
        throw new TypeError('Title must be an array.');
    }

    this._title= title;

    return this;
};

/**
 * Getter/Setter for a CWContact mail.
 *
 * @param mail When provided, this method sets mail as the new CWContact instance mail.
 * @returns {*} Returns the CWContact instance the _mail attribute refers to when a mail is provided.
 * When it is not, it directly returns the instance mail value.
 */
CWContact.prototype.mail = function (mail) {
    if (mail === undefined) {
        return this._mail;
    }

    if (mail && typeof mail !== 'string') {
        throw new TypeError('Mail must be a string.');
    }

    this._mail = mail;

    return this;
};

/**
 * Getter/Setter for a CWContact tel.
 *
 * @param tel When provided, this method sets tel as the new CWContact instance tel.
 * @returns {*} Returns the CWContact instance the _tel attribute refers to when a tel is provided.
 * When it is not, it directly returns the instance tel value.
 */
CWContact.prototype.tel = function (tel) {
    if (tel === undefined) {
        return this._tel;
    }

    if (tel && typeof tel !== 'string') {
        throw new TypeError('Tel must be a string.');
    }

    this._tel = tel;

    return this;
};

module.exports = CWContact;

/**
* Here are the functions Union and Compare
*/

CWCOntact.prototype.union = function (contact){

	if ((this.name == contact.name) && (this.firstname == contact.firstname)){   // we merge contacts only when the have the same fullname.
		this.adress = (this.adress == "") ? contact.adress : "" ;   				 //we replace a property only when it's firstly empty.
		this.organisation.push(contact.organisation) ;								// here we push the array to add a new organisation
		this.title.push(contact.title) ;
		this.mail = (this.mail == "") ? : contact.mail : "" ;
		this.tel = (this.tel == "") ? contact.tel : "" ;
	}else{
		alert("the two contacts do not have the same fullname");
	}
	
CWContact.prototype.compare = function (contact){
	
	var differencies = [];
	var similarities = [];
	
	this.name == contact.name ? similarities.push("name") : differencies.push(name);		//push the differences or similitaries in the right arrays
	this.firstName == contact.firstName ? similarities.push("firstName") : differencies.push("firstName");
	this.organisation == contact.organisation ? similarities.push("organisation") : differencies.push("organisation");
	this.title == contact.title ? similarities.push("title") : differencies.push("title");
	this.adress == contact.adress ? similarities.push("adress") : differencies.push("adress");
	this.mail == contact.mail ? similarities.push("mail") : differencies.push("mail");
	this.tel == contact.tel ? similarities.push("tel") : differencies.push("tel");
	
	return differencies, similarities ;			// ...how to return both?
	
	CWContact.prototype.export = function(){
	
		return (this.name + "; " + "this.firstName + "; " + this.organisation + "; " + "this.title + "; " + this.tel + "; " + this.address + "; ");
	}
		